<template>
  <div class="carousel-manager">
    <!-- 标题区域 -->
    <div class="manager-header">
      <div class="title-container">
        <i class="el-icon-picture-outline-round header-icon"></i>
        <h2 class="header-title">轮播图管理</h2>
      </div>
      <el-button 
        size="small" 
        icon="el-icon-refresh" 
        @click="getList"
        class="refresh-btn"
      >刷新列表</el-button>
    </div>

    <!-- 上传区域 -->
    <div class="upload-wrapper">
      <el-card shadow="never" class="upload-card">
        <el-upload
          action=""
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-upload="beforeUpload"
          :file-list="fileList"
          list-type="picture-card"
          :class="{ 'upload-disabled': fileList.length >= 10 }"
        >
          <div class="upload-area">
            <i class="el-icon-plus upload-icon"></i>
            <div class="upload-text">点击上传</div>
          </div>
          <div slot="tip" class="upload-tip">
            支持 JPG/PNG 格式，建议尺寸 1920×800px，大小不超过500KB
            <span v-if="fileList.length >= 10" class="upload-limit">(已达10张上限)</span>
          </div>
        </el-upload>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fileList: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    beforeUpload(file) {
      let params = new FormData()
      params.append('file', file)
      this.$axios.post('/api/carousel/addImg', params,{headers: {'Content-Type': 'multipart/form-data'}})
        .then(res => {
          console.log(res)
          if (res.data.code == '001') {
            this.fileList.push({
              id: res.data.data.id,
              name: res.data.data.intro,
              url: 'http://localhost:3000/'+res.data.data.imgPath,
              status: "success"
            })
            this.notifySucceed(res.data.msg)
          } else {
            this.notifyError(res.data.msg)
          }
        })
    },
    // 删除图片
    handleRemove(file) {
      console.log(file)
      this.$axios.post('/api/carousel/deleteImg', {
        id: file.id
      })
        .then(res => {
          if (res.data.code == '001') {
            this.$notifySucceed(res.data.msg)
          } else {
            this.uploadImg(this.row)
            this.$notifyError(res.data.msg)
          }
        })
    },
    // 预览图片
    handlePreview(file) {
      window.open(file.url)
    },
    getList() {
      this.$axios.post('/api/carousel/getList') 
        .then(res => {
          console.log(res)
          res.data.data.forEach(item => {
            this.fileList.push({
              id: item.carousel_id,
              name: item.describes,
              url: 'http://localhost:3000/'+item.imgPath,
              status: "success"
            })
          })
        })
    }
  }
}
</script>

<style lang="less" scoped>
.carousel-manager {
  padding: 24px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 84px);
}

.manager-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  padding: 16px 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
  
  .title-container {
    display: flex;
    align-items: center;
    
    .header-icon {
      font-size: 24px;
      color: #409EFF;
      margin-right: 12px;
    }
    
    .header-title {
      margin: 0;
      font-size: 18px;
      font-weight: 600;
      color: #303133;
    }
  }
  
  .refresh-btn {
    padding: 8px 16px;
    border-radius: 4px;
    background-color: #f5f7fa;
    border-color: #dcdfe6;
    
    &:hover {
      background-color: #e6e9f0;
    }
  }
}

.upload-wrapper {
  .upload-card {
    border-radius: 8px;
    border: none;
    background-color: #fff;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
    
    ::v-deep .el-upload-list--picture-card {
      .el-upload-list__item {
        width: 180px;
        height: 120px;
        border-radius: 6px;
        transition: all 0.3s ease;
        border: 1px solid #e6ebf5;
        
        &:hover {
          transform: translateY(-3px);
          box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
          border-color: #c0c4cc;
        }
      }
    }
    
    ::v-deep .el-upload--picture-card {
      width: 180px;
      height: 120px;
      line-height: normal;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      border-radius: 6px;
      background-color: #fafafa;
      border: 1px dashed #d9d9d9;
      transition: all 0.3s ease;
      
      &:hover {
        border-color: #409EFF;
        background-color: #f0f7ff;
      }
    }
    
    .upload-area {
      display: flex;
      flex-direction: column;
      align-items: center;
      
      .upload-icon {
        font-size: 28px;
        color: #8c939d;
        margin-bottom: 8px;
      }
      
      .upload-text {
        font-size: 14px;
        color: #606266;
      }
    }
    
    .upload-tip {
      margin-top: 12px;
      font-size: 13px;
      color: #909399;
      text-align: center;
      
      .upload-limit {
        color: #f56c6c;
        margin-left: 4px;
      }
    }
  }
}

.upload-disabled {
  ::v-deep .el-upload--picture-card {
    cursor: not-allowed;
    opacity: 0.6;
    
    &:hover {
      border-color: #d9d9d9;
      background-color: #fafafa;
    }
  }
}

@media (max-width: 768px) {
  .manager-header {
    flex-direction: column;
    align-items: flex-start;
    
    .refresh-btn {
      margin-top: 12px;
      width: 100%;
    }
  }
  
  .upload-wrapper {
    ::v-deep .el-upload-list--picture-card,
    ::v-deep .el-upload--picture-card {
      width: 140px;
      height: 100px;
    }
  }
}
</style>